package com.ruoyi.controller.quality;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.domain.quality.MesQualityInspectionItem;
import com.ruoyi.service.quality.IMesQualityInspectionItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 检验项目记录Controller
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/quality/inspectionItem")
public class MesQualityInspectionItemController extends BaseController
{
    @Autowired
    private IMesQualityInspectionItemService IMesQualityInspectionItemService;

    /**
     * 查询检验项目记录列表
     */
    @PreAuthorize("@ss.hasPermi('quality:inspectionItem:list')")
    @GetMapping("/list")
    public TableDataInfo list(MesQualityInspectionItem mesQualityInspectionItem)
    {
        startPage();
        List<MesQualityInspectionItem> list = IMesQualityInspectionItemService.selectMesQualityInspectionItemList(mesQualityInspectionItem);
        return getDataTable(list);
    }

    /**
     * 导出检验项目记录列表
     */
    @PreAuthorize("@ss.hasPermi('quality:inspectionItem:export')")
    @Log(title = "检验项目记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MesQualityInspectionItem mesQualityInspectionItem)
    {
        List<MesQualityInspectionItem> list = IMesQualityInspectionItemService.selectMesQualityInspectionItemList(mesQualityInspectionItem);
        ExcelUtil<MesQualityInspectionItem> util = new ExcelUtil<MesQualityInspectionItem>(MesQualityInspectionItem.class);
        util.exportExcel(response, list, "检验项目记录数据");
    }

    /**
     * 获取检验项目记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('quality:inspectionItem:query')")
    @GetMapping(value = "/{itemId}")
    public AjaxResult getInfo(@PathVariable("itemId") Long itemId)
    {
        return success(IMesQualityInspectionItemService.selectMesQualityInspectionItemByItemId(itemId));
    }

    /**
     * 新增检验项目记录
     */
    @PreAuthorize("@ss.hasPermi('quality:inspectionItem:add')")
    @Log(title = "检验项目记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MesQualityInspectionItem mesQualityInspectionItem)
    {
        return toAjax(IMesQualityInspectionItemService.insertMesQualityInspectionItem(mesQualityInspectionItem));
    }

    /**
     * 修改检验项目记录
     */
    @PreAuthorize("@ss.hasPermi('quality:inspectionItem:edit')")
    @Log(title = "检验项目记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MesQualityInspectionItem mesQualityInspectionItem)
    {
        return toAjax(IMesQualityInspectionItemService.updateMesQualityInspectionItem(mesQualityInspectionItem));
    }

    /**
     * 删除检验项目记录
     */
    @PreAuthorize("@ss.hasPermi('quality:inspectionItem:remove')")
    @Log(title = "检验项目记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{itemIds}")
    public AjaxResult remove(@PathVariable Long[] itemIds)
    {
        return toAjax(IMesQualityInspectionItemService.deleteMesQualityInspectionItemByItemIds(itemIds));
    }

    /**
     * 根据检验记录ID查询检验项目记录列表
     */
    @GetMapping("/inspection/{inspectionId}")
    public AjaxResult getItemsByInspectionId(@PathVariable Long inspectionId)
    {
        List<MesQualityInspectionItem> list = IMesQualityInspectionItemService.selectItemsByInspectionId(inspectionId);
        return success(list);
    }

    /**
     * 根据检验单号查询检验项目记录列表
     */
    @GetMapping("/inspectionCode/{inspectionCode}")
    public AjaxResult getItemsByInspectionCode(@PathVariable String inspectionCode)
    {
        List<MesQualityInspectionItem> list = IMesQualityInspectionItemService.selectItemsByInspectionCode(inspectionCode);
        return success(list);
    }

    /**
     * 根据检验项目ID查询检验项目记录列表
     */
    @GetMapping("/item/{itemId}")
    public AjaxResult getRecordsByItemId(@PathVariable Long itemId)
    {
        List<MesQualityInspectionItem> list = IMesQualityInspectionItemService.selectRecordsByItemId(itemId);
        return success(list);
    }

    /**
     * 根据检验结果查询检验项目记录列表
     */
    @GetMapping("/result/{result}")
    public AjaxResult getItemsByResult(@PathVariable String result)
    {
        List<MesQualityInspectionItem> list = IMesQualityInspectionItemService.selectItemsByResult(result);
        return success(list);
    }

    /**
     * 批量新增检验项目记录
     */
    @PreAuthorize("@ss.hasPermi('quality:inspectionItem:add')")
    @Log(title = "检验项目记录", businessType = BusinessType.INSERT)
    @PostMapping("/batch")
    public AjaxResult addBatch(@RequestBody List<MesQualityInspectionItem> inspectionItems)
    {
        return toAjax(IMesQualityInspectionItemService.insertBatchInspectionItems(inspectionItems));
    }
}